package com.project.ssmfo.controller.f;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.project.ssmfo.common.Configuration;
import com.project.ssmfo.controller.ActivityController;
import com.project.ssmfo.controller.BaseController;
import com.project.ssmfo.model.Activity;
import com.project.ssmfo.model.ActivityEvaluate;
import com.project.ssmfo.model.ActivityStudent;
import com.project.ssmfo.model.FrontUser;
import com.project.ssmfo.service.ActivityEvaluateService;
import com.project.ssmfo.service.ActivityService;
import com.project.ssmfo.service.ActivityStudentService;
import com.project.ssmfo.service.FrontUserService;
import com.project.utils.PaginationInfo;
/**
 * 前台controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/f")
public class FrontController extends BaseController{
	static final Logger log = LoggerFactory.getLogger(FrontController.class);
	/**
	 * @Fields SPLITPAGE_SIZE : 活动管理分页，每页的数目
	 */
	public static final int SPLITPAGE_SIZE = 10;
	
	@Autowired
	ActivityService activityService;
		
	@Autowired
	FrontUserService frontUserService;
	
	@Autowired
	ActivityEvaluateService activityEvaluateService;
	
	@Autowired
	ActivityStudentService activityStudentService;

	
	@RequestMapping(value = "index")
	public String index(Model model) {		
		return "redirect:/f/activityList";
	}
	
	@RequestMapping(value = "toLogin")
	public String toLogin() {
		return "front/login";
	}

	@RequestMapping(value = "login")
	public String login(Model model,HttpServletRequest request,FrontUser frontUser) {
		frontUser = frontUserService.getByCondition(frontUser);
		HttpSession e=request.getSession();
    	if (frontUser != null) {
    		e.setAttribute("frontUser", frontUser);
    		return "redirect:/f/selfCenter";
			} else {
			model.addAttribute("message", "用户名或密码错误");
			return "front/login";
		}	
	}
	@RequestMapping(value = "toRegister")
	public String toRegister() {
		return "front/register";
	}
	
	@RequestMapping(value = "register")
	@ResponseBody
	public String register(Model model,FrontUser frontUser) {
		frontUser.setCreateTime(new Date());
		frontUser.setDeleted(false);
		try {
			frontUserService.insert(frontUser);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message", "注册失败");
			return "fail";
		}
		model.addAttribute("message", "注册成功");
		return "success";

	}
	
	@RequestMapping(value = "selfCenter")
	public String selfCenter() {
		return "front/selfCenter";
	}
	
	@RequestMapping(value = "saveFrontUser")
	@ResponseBody
	public String saveFrontUser(Model model,FrontUser frontUser) {
		try {
			frontUserService.updateByPrimaryKeySelective(frontUser);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message", "修改失败");
			return "fail";
		}
		model.addAttribute("message", "修改成功");
		return "success";
	}
	
	@RequestMapping(value = "activityList")
	public String activityList(HttpServletRequest request,Model model, Integer reqPage, String keyword) {
		
		try {
			//获取请求参数
			if (reqPage == null) {
				reqPage = 1;
			}

			Map<String, Object> params = new HashMap<String, Object>();
			if (!StringUtils.isEmpty(keyword)) {
				params.put("keyword", keyword);
			}
			FrontUser frontUser=(FrontUser) request.getSession().getAttribute("frontUser");
			params.put("frontUserId", frontUser.getId());
			
			PaginationInfo<Activity> pageinfo = activityService.getPaginationData(params, reqPage, SPLITPAGE_SIZE);
			model.addAttribute("pageinfo", pageinfo);
			model.addAttribute("keyword", keyword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "front/activityList";
	}
	
	/**
	 * <p>Title: logout</p>
	 * <p>Description: 退出</p>
	 * @param model
	 * @param request
	 * @param frontUser
	 * @return String
	 */
	@RequestMapping(value = "logout")
	public String logout(Model model,HttpServletRequest request) {
		HttpSession e=request.getSession();
    	e.removeAttribute("frontUser");
		return "front/login";
	}
	
	/**
	 * <p>Title: activityDetail</p>
	 * <p>Description: 活动详情页面</p>
	 * @param id 活动ID
	 * @return String
	 */
	@RequestMapping("activityDetail")
	public String activityDetail(Model model, Integer id){
		log.info("[detail] activity detail .");
			Activity activity = activityService.selectByPrimaryKey(id);
			model.addAttribute("activity", activity);
			List<ActivityEvaluate> list=activityEvaluateService.findByActivityId(id);
			model.addAttribute("evaluateList", list);
			return "front/activityDetail";
		
	}
	
	/**
	 * <p>Title: attendForm</p>
	 * <p>Description: 报名</p>
	 * @param model
	 * @param id
	 * @return String
	 */
	@RequestMapping(value = "attendForm")
	public String attendForm(Model model,Integer id) {
		Activity activity = activityService.selectByPrimaryKey(id);
		model.addAttribute("activity", activity);
		return "front/attend";
	}
	
	/**
	 * <p>Title: attend</p>
	 * <p>Description: 报名信息提交</p>
	 * @param model
	 * @param id
	 * @param activityStudent
	 * @return String
	 */
	@RequestMapping(value = "attend")
	@ResponseBody
	public String attend(Model model,ActivityStudent activityStudent) {
		activityStudent.setCreateTime(new Date());
		activityStudent.setUpdateTime(new Date());
		activityStudent.setDeleted(false);
		activityStudentService.insertSelective(activityStudent);
		return "success";
	}
	
	@RequestMapping("saveEvaluate")
	@ResponseBody
	public String saveEvaluate(ActivityEvaluate comment) {
		try {
			comment.setDeleted(false);
			comment.setCreateTime(new Date());
			activityEvaluateService.insert(comment);
			return JSON.toJSONString(comment);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "fail";
	}
	
}
